package com.hightrast.service.util;

import com.hightrast.service.api.baseEntity.BaseEntity;
import com.hightrast.service.api.entity.RastUser;

import java.time.LocalDateTime;

public class BaseEntityUtil {
    public BaseEntityUtil() {
    }

    public static <T extends BaseEntity> T save(T entity) {
        return save(entity, (RastUser)null);
    }

    public static <T extends BaseEntity> T save(T entity, RastUser user) {
        return save(entity, user, LocalDateTime.now());
    }

    public static <T extends BaseEntity> T save(T entity, RastUser user, LocalDateTime date) {
        if (user != null) {
            entity.setCreateId(user.getId());
            entity.setModifyId(user.getId());
        }

        entity.setCreateTime(date);
        entity.setModifyTime(date);
        entity.setIsDeleted(0);
        return entity;
    }

    public static <T extends BaseEntity> T update(T entity) {
        return update(entity, (RastUser)null);
    }

    public static <T extends BaseEntity> T update(T entity, RastUser user) {
        return update(entity, user, LocalDateTime.now());
    }

    public static <T extends BaseEntity> T update(T entity, RastUser user, LocalDateTime date) {
        if (user != null) {
            entity.setModifyId(user.getId());
        }

        entity.setModifyTime(date);
        return entity;
    }
}